package com.example.testeviews;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);

		TypedArray array = getResources().obtainTypedArray(R.array.planets);
		final Drawable [] drawables = new Drawable[array.length()];
		for (int i = 0; i < array.length(); i++) {
			drawables[i] = array.getDrawable(i); 
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.spinner_textview,
				getResources().getStringArray(R.array.planetNames));

		adapter.setDropDownViewResource(R.layout.simple_textview);
		
		final ImageView img = (ImageView)findViewById(R.id.img);

		Spinner spinner = (Spinner) findViewById(R.id.comboPlanetas);
		spinner.setAdapter(adapter);

		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				img.setImageDrawable(drawables[position]);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) { }
		});
	}
}
